const app = getApp();
const api = require('../../../ws_micro_shop/utils/api.js');
const utils = require('../../../ws_micro_shop/utils/utils.js');
const { qyWXCheckSeesion } = require('../../../common/utils/ws/ws-wxwork');

Page({
  data: {
    abnorData: {
      imgSrc: app.imgUrl + 'abnor-15.png',
      text: '暂无可分享优惠券~',
    },
    list: [],
    isData: false,
    page: 1,
    canPullUp: true,
    loadingText: '下拉加载',
    share_modal_active: '',
    imgUrl: app.imgUrl
  },

  onLoad(options) {
    wx.hideShareMenu();
    let that = this;
    that.setData(
      {
        coupon_id: options.id,
        stroeId: options.shopid,
        rolesId: options.guide_id,
        share_from: options.from,
        isSaas: app.ws_micro_shop_globalData.isSaas,
      },
      () => {
        app.ws_micro_shoplogin().then(res => {
          that.getCoupon();
          app.isWXWork && qyWXCheckSeesion({});
        });
        
      },
    );
  },

  getCoupon() {
    wx.showLoading({
      title: '正在加载...',
      mask: true,
    });

    const { stroeId: shop_id = '', page, list } = this.data;
    app.ws_micro_shoprequest({
      url: api.staff.coupon_list,
      data: {
        c_type: 1,
        shop_id,
        page,
      },
      success: res => {
        if (res && res.code === 0 && res.data) {
          const newList = res.data.list;
          if (newList.length <= 0) {
            utils.after(res, this);
            return;
          }
          this.setData({
            list: [...list, ...newList],
          }, () => {
            utils.after(res, this);
          });
        }
      },
      fail: res => {
        utils.after(res, this);
      },
      complete: res => {
        this.setData({
          isData: true,
        }, () => {
          wx.hideLoading();
        });
      },
    });
  },

  onReachBottom() {
    let { canPullUp, page } = this.data;

    if (canPullUp) {
      this.setData(
        {
          page: ++page,
        },
        () => {
          this.getCoupon();
        },
      );
    }
  },

  onShareAppMessage(res) {
    // if (res.from === 'button') {
      const { stroeId = '', rolesId = '', coupon_id } = this.data;
      const imageUrl = `${app.siteInfo.domain}/addons/ws_micro_shop/core/web/statics/images/guide_coupon.png`;
      const { module: moduleName } = app.siteInfo;
      const path = `/ws_micro_shop/pages/index/index?id=${coupon_id}&guide_id=${rolesId}&shopid=${stroeId}`;
      return {
        path: app.util.spliceSharePath(path, '4', moduleName),
        title: '',
        imageUrl,
      };
    // }
  },

  onUserOpStatistic(e) {
    // if (String(e.op) === 'share') {
      // const type = 0;
      // const { coupon_id: param_id = '' } = this.data;
      // app.handleStatisticsShare({ type, param_id });
    // }
      // wx.hideLoading();
  },

  changesharecoupon(e) {
    let dataset = e.currentTarget.dataset;
    this.setData(
      dataset
    )
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0, // android机子滑动会从上往下，动画时长为0可以暂时解决，不过体验不太好
    });
  }
});
